package com.wontube.client.pub;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import com.wontube.client.json.JsonReader;
import com.wontube.client.json.JsonToken;
import com.wontube.client.json.JsonWriter;


/**
 * @author yuechao
 * 
 */
public final class ServerResponse implements Serializable {
	private static final long serialVersionUID = 1L;

	private boolean isDone;
	private String sessionId;
	private String content;
	private HashMap<String, String> headers = new HashMap<String, String>();
	
	public boolean isDone() {
		return this.isDone;
	}

	public String getSessionId() {
		return this.sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}
	
	public void setDone(boolean isDone) {
		this.isDone = isDone;
	}

	public String getContent() {
		return this.content;
	}

	public String toJsonString(){
		StringWriter sw = new StringWriter();
		JsonWriter jsonWriter = new JsonWriter(sw);
		try {
			this.write(jsonWriter);
			jsonWriter.flush();
			return sw.toString();
		} catch (IOException e) {
		} finally {
			try {
				jsonWriter.close();
			} catch (IOException e) {
			}
		}
		
		return "";
	}
	
	public ServerResponse fromJsonString(String json) {
		JsonReader reader = new JsonReader(new StringReader(json));
		try {
			this.read(reader);
		} catch (IOException e) {
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
			}
		}
		
		return this;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public HashMap<String, String> getHeaders() {
		return this.headers;
	}
	
	public void write(JsonWriter writer) throws IOException {
		writer.beginObject();
		writer.name("isDone").value(this.isDone);
		writer.name("sessionId").value(this.sessionId);
		writer.name("headers");
		this.writeHeaders(writer);
		if (this.content != null) {
			writer.name("content").value(this.content);
		}
		writer.endObject();
	}
	
	public void read(JsonReader reader) throws IOException {
		reader.beginObject();
		while (reader.hasNext()) {
			String name = reader.nextName();
			if ("isDone".equals(name)) {
				this.isDone = reader.nextBoolean();
			} else if ("sessionId".equals(name)) {
				JsonToken token = reader.peek();
				if (token == JsonToken.NULL) {
					reader.nextNull();
				} else {
					this.sessionId = reader.nextString();
				}
			} else if ("content".equals(name)) {
				this.content = reader.nextString();
			} else if ("headers".equals(name)) {
				this.readHeaders(reader);
			} else {
				reader.skipValue();
			}
		}
		reader.endObject();
	}
	
	private void writeHeaders(JsonWriter writer) throws IOException {
		writer.beginObject();
		for (Map.Entry<String, String> entry : this.headers.entrySet()) {
			writer.name(entry.getKey()).value(entry.getValue());
		}
		writer.endObject();
	}

	/**
	 * @param reader
	 * @throws IOException 
	 */
	private void readHeaders(JsonReader reader) throws IOException {
		reader.beginObject();
		this.headers.clear();
		while (reader.hasNext()) {
			String name = reader.nextName();
			String value = reader.nextString();
			this.headers.put(name, value);
		}
		reader.endObject();
	}
}
